package com.xmb.wechat.delegate;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.bean.AliPayBillBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.util.AliPayDateUtils;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.util.PicLoadUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/27 11:19
 */
public class AliPayBillDetailDelegate extends BaseRecyclerViewManager {

    public AliPayBillDetailDelegate(Activity activity) {
        super(activity);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new AliPayBillDetailAdapter(new ArrayList<>());
    }

    class AliPayBillDetailAdapter extends BaseMultiItemQuickAdapter<AliPayBillBean, BaseViewHolder> {

        AliPayBillDetailAdapter(List<AliPayBillBean> data) {
            super(data);
            addItemType(777, R.layout.item_alipay_bill_detail_time);
            addItemType(Constant.AliPayBillType.TYPE_ZDY, R.layout.item_alipay_bill_common);
            addItemType(Constant.AliPayBillType.TYPE_WG, R.layout.item_alipay_bill_common);
            addItemType(Constant.AliPayBillType.TYPE_TX, R.layout.item_alipay_bill_common);
            addItemType(Constant.AliPayBillType.TYPE_HB, R.layout.item_alipay_bill_common);
            addItemType(Constant.AliPayBillType.TYPE_ZZ, R.layout.item_alipay_bill_common);
            addItemType(Constant.AliPayBillType.TYPE_SJCZ, R.layout.item_alipay_bill_common);
            addItemType(Constant.AliPayBillType.TYPE_HBHK, R.layout.item_alipay_bill_common);
        }

        @Override
        protected void convert(BaseViewHolder helper, AliPayBillBean item) {
            if (item.getType() == 777) {//时间
                helper.setText(R.id.tv_time, TextUtils.isEmpty(item.getMonthInYear()) ? "" : item.getMonthInYear());
                helper.setText(R.id.tv_expend, TextUtils.isEmpty(item.getExpend()) ? "" : item.getExpend());
                helper.setText(R.id.tv_income, TextUtils.isEmpty(item.getIncome()) ? "" : item.getIncome());
            } else {//通用
                PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), helper.getView(R.id.iv_icon));
                helper.setText(R.id.tv_title, TextUtils.isEmpty(item.getTitle()) ? "" : item.getTitle());
                helper.setText(R.id.tv_classify, TextUtils.isEmpty(item.getBillClassify()) ? "" : item.getBillClassify());
                helper.setText(R.id.tv_time, AliPayDateUtils.getMonthDate(item.getTradeTime()));
                //money
                switch (item.getType()) {
                    case Constant.AliPayBillType.TYPE_ZDY:
                        showMoneyWithDifferentColor(helper, item);
                        helper.setVisible(R.id.tv_state, true);
                        break;
                    case Constant.AliPayBillType.TYPE_WG:
                        showMoneyWithDifferentColor(helper, item);
                        helper.setVisible(R.id.tv_state, true);
                        break;
                    case Constant.AliPayBillType.TYPE_TX:
                        showMoneyWithSameColor(helper, item);
                        helper.setVisible(R.id.tv_state, true);
                        break;
                    case Constant.AliPayBillType.TYPE_HB:
                        showMoneyWithDifferentColor(helper, item);
                        helper.setVisible(R.id.tv_state, true);
                        break;
                    case Constant.AliPayBillType.TYPE_ZZ:
                        showMoneyWithDifferentColor(helper, item);
                        helper.setVisible(R.id.tv_state, true);
                        break;
                    case Constant.AliPayBillType.TYPE_SJCZ:
                        showMoneyWithDifferentColor(helper, item);
                        helper.setVisible(R.id.tv_state, false);
                        break;
                    case Constant.AliPayBillType.TYPE_HBHK:
                        showMoneyWithDifferentColor(helper, item);
                        helper.setVisible(R.id.tv_state, false);
                        break;
                }
                showState(helper, item);
                setFont(mContext, helper);
            }
        }
    }

    private void showState(BaseViewHolder helper, AliPayBillBean item) {
        switch (item.getTradeState()) {
            case 0:
                helper.setTextColor(R.id.tv_state, context.getResources().getColor(R.color.red_orange_4));
                break;
            default:
                helper.setTextColor(R.id.tv_state, context.getResources().getColor(R.color.color_9));
                break;
        }
        helper.setText(R.id.tv_state, TextUtils.isEmpty(item.getTradeStateValue()) ? "" : item.getTradeStateValue());
    }

    private void setFont(Context context, BaseViewHolder helper) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/alipay_number.ttf");
        TextView tvMoney = helper.getView(R.id.tv_money);
        if (tf != null)
            tvMoney.setTypeface(tf);
    }

    /**
     * 收入与支出不一样颜色
     *
     * @param helper
     * @param item
     */
    private void showMoneyWithDifferentColor(BaseViewHolder helper, AliPayBillBean item) {
        if (item.isExpend()) {
            helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.color_3));
            helper.setText(R.id.tv_money, "-" + MoneyFormat.formatMoney(item.getMoney()));
        } else {
            helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.red_orange_3));
            helper.setText(R.id.tv_money, "+" + MoneyFormat.formatMoney(item.getMoney()));
        }
    }

    /**
     * 收入与支出颜色一样
     *
     * @param helper
     * @param item
     */
    private void showMoneyWithSameColor(BaseViewHolder helper, AliPayBillBean item) {
        helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.color_3));
        helper.setText(R.id.tv_money, MoneyFormat.formatMoney(item.getMoney()));
    }

    @Override
    protected RecyclerView.ItemDecoration getItenDecoration() {
        return null;
    }
}
